package com.tlgen.modules.dept.controller;

import com.tlgen.common.exception.BadRequestException;
import com.tlgen.modules.dept.domain.Subject;
import com.tlgen.modules.dept.service.SubjectService;
import com.tlgen.modules.dept.service.dto.SubjectDto;
import com.tlgen.modules.dept.service.dto.SubjectQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(tags = "系统：科室")
@RestController
@RequestMapping("/api/subject")
@SuppressWarnings("unchecked")
public class SubjectController {

    private final SubjectService subjectService;

    private static final String ENTITY_NAME = "subject";

    public SubjectController(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    @ApiOperation("返回全部的tree,下拉使用")
    @GetMapping(value = "/tree")
    @PreAuthorize("@tlgen.check('subject:list')")
    public ResponseEntity<Object> getSubjectTree(){
        return new ResponseEntity<>(subjectService.getSubjectTree(subjectService.findByPid(0L)), HttpStatus.OK);
    }


    @ApiOperation("查询列表")
    @GetMapping
    @PreAuthorize("@tlgen.check('subject:list')")
    public ResponseEntity<Object> getMenus(SubjectQueryCriteria criteria){
        List<SubjectDto> jAreaDtoList = subjectService.queryAll(criteria);
        return new ResponseEntity<>(subjectService.buildTree(jAreaDtoList), HttpStatus.OK);
    }

    @ApiOperation("新增")
    @PostMapping
    @PreAuthorize("@tlgen.check('subject:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Subject resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        return new ResponseEntity<>(subjectService.create(resources), HttpStatus.CREATED);
    }


    @ApiOperation("修改")
    @PutMapping
    @PreAuthorize("@tlgen.check('subject:edit')")
    public ResponseEntity<Object> update(@Validated(Subject.Update.class) @RequestBody Subject resources){
        subjectService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除")
    @DeleteMapping
    @PreAuthorize("@tlgen.check('subject:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
        Set<Subject> subjectet = new HashSet<>();
        for (Long id : ids) {
            List<Subject> subjectList = subjectService.findByPid(id);
            subjectet.add(subjectService.findOne(id));
            subjectet = subjectService.getDeleteSubjects(subjectList, subjectet);
        }
        subjectService.delete(subjectet);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@tlgen.check('subject:list')")
    public void download(HttpServletResponse response, SubjectQueryCriteria criteria) throws IOException {
        subjectService.download(subjectService.queryAll(criteria), response);
    }

}
